<!--
 * Copyright (c) 2018-2025, duneng All rights reserved.
 *
 * Author: wq (meteor85@163.com) 
 * 2018年9月17日 只修改了部分展现要素，为了能让界面能展现。待后台接口完备后，再做细节处理。目前仅为静态数据
-->

<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit" v-if="sensorManage_Alert_btn_add">添加
      </el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="正在努力加载中..." border fit highlight-current-row style="width: 99%">

      <el-table-column align="center" label="序号">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>

      <el-table-column label="传感器名称">
        <template slot-scope="scope">
          <span>{{scope.row.sensorName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="传感器阈值">
        <template slot-scope="scope">
          <span>{{scope.row.sensorValue}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="success" v-if="sensorManage_Alert_btn_edit" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button size="mini" type="danger" v-if="sensorManage_Alert_btn_del" @click="handleDelete(scope.row)">删除
          </el-button>
          <!-- <el-button size="mini" type="info" plain @click="handlePermission(scope.row)" v-if="roleManager_btn_perm">权限
          </el-button> -->
        </template>
      </el-table-column>

    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="传感器名称" prop="sensorName">
          <el-input v-model="form.sensorName" placeholder="传感器名称"></el-input>
        </el-form-item>
        <el-form-item label="传感器阈值" prop="sensorValue">
          <el-input v-model="form.sensorValue" placeholder="传感器阈值"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>

    <!--<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogDeptVisible">
      <el-tree class="filter-tree" :data="treeDeptData" :default-checked-keys="checkedKeys" check-strictly node-key="id" highlight-current ref="deptTree" @node-click="getNodeData" :props="defaultProps" :filter-node-method="filterNode" default-expand-all>
      </el-tree>
    </el-dialog>-->

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogPermissionVisible">
      <el-tree class="filter-tree" :data="treeData" :default-checked-keys="checkedKeys" check-strictly node-key="id" highlight-current :props="defaultProps" show-checkbox ref="menuTree" :filter-node-method="filterNode" default-expand-all>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="Permession(sensorId, sensorType)">更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addObj,
  fetchList,
  getObj,
  putObj,
  delObj
} from '@/api/bus/sensor/alert';

import { fetchTree } from '@/api/menu';
import { mapGetters } from 'vuex';
import waves from '@/directive/waves/index.js'; // 水波纹指令

export default {
  name: 'table_sensorValue',
  directives: {
    waves
  },
  data() {
    return {
      treeData: [],
      treeDeptData: [],
      checkedKeys: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      form: {
        sensorName: undefined,
        sensorValue: undefined,
        // roleDesc: undefined,
        // deptName: undefined,
        // roleDeptId: undefined
      },
      //roleId: undefined,
      //roleCode: undefined,
      rules: {
        sensorName: [
          {
            required: true,
            message: '传感器名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        sensorValue: [
          {
            required: true,
            message: '传感器阈值',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 6,
            message: '长度在 1 到 6 位数',
            trigger: 'blur'
          }
        ],
      },
      statusOptions: ['0', '1'],
      //rolesOptions: undefined,
      dialogFormVisible: false,
      dialogDeptVisible: false,
      dialogPermissionVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建',
        permission: '分配权限'
      },
      tableKey: 0,
      tableKey: 0,
      sensorManage_Alert_btn_add: false,
      sensorManage_Alert_btn_edit: false,
      sensorManage_Alert_btn_del: false,
      // sensorManage_Alert_btn_perm: false
    }
  },
  created() {
    this.getList();
    console.log(this.permissions);
    this.sensorManage_Alert_btn_add = this.permissions['sys_sensorAlert_add']
    this.sensorManage_Alert_btn_edit = this.permissions['sys_sensorAlert_edit']
    this.sensorManage_Alert_btn_del = this.permissions['sys_sensorAlert_del']
    // this.sensorManage_Alert_btn_perm = this.permissions['sys_sensorAlert_perm']
  },
  computed: {
    ...mapGetters(['elements', 'permissions'])
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create';
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        this.form = response.data
        this.dialogFormVisible = true
        this.dialogStatus = 'update';
      })
    },
    
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },

    handleDelete(row) {
      delObj(row.id).then(response => {
        this.dialogFormVisible = false
        this.getList()
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    create(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          console.log(this.form);
          putObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    resetTemp() {
      this.form = {
        id: undefined,
        sensorName: undefined,
        sensorValue: undefined,
      }
    }
  }
}
</script>
